﻿using System;
using System.Collections.Generic;
using System.IO;

namespace OrbClientCacher
{
	
	/// <summary>
	/// Settings der Anwendung werden in diesem Struct gespeichert
	/// </summary>
	public struct OrbCCSettings
	{
		/// <summary>
		/// Index der ASPX-Seiten (temporär)
		/// </summary>
		public IteratingList<Site> AspxIndex { get; set; }

		/// <summary>
		/// Index der ASMX-Seiten (statisch)
		/// </summary>
		public IteratingList<string> AsmxIndex { get; set; }

		/// <summary>
		/// Blacklist mit zu ignorierenden Verzeichnissen
		/// </summary>
		public IList<string> BlackList { get; set; }

		
		/// <summary>
		/// Intervall in Sekunden in dem der Cache geprüft werden soll
		/// </summary>
		public int CachingTimer { get; set; }

		/// <summary>
		/// Liste aller Entity-Typecodes die auf Site-Objekte mit DoEtc=true angewendet werden
		/// </summary>
		public IList<int> EtcList { get; set; }

		/// <summary>
		/// Intervall in Sekunden, in dem der Host-Service geprüft wird
		/// </summary>
		public int HostCheckTimer { get; set; }

		/// <summary>
		/// Initiale Zeit, die der Cacher warten soll, bevor es losgeht
		/// </summary>
		public int InitialCacheWaitTime { get; set; }

		/// <summary>
		/// Flag welches angibt, ob alle ASPX-Seiten oder nur die aus der Top-Liste gechached werden sollen
		/// </summary>
		public bool SmartCache { get; set; }

		/// <summary>
		/// Größe des Threadpools, mit dem gearbeitet wird
		/// </summary>
		public int Threads { get; set; }

		/// <summary>
		/// Zeit in Millisekunden, die zwischen Anfragen an den Host gewartet werden soll
		/// </summary>
		public int Throttle { get; set; }

		/// <summary>
		/// Liste der zu indizierenden Seiten mit Priorität 1 und 2
		/// </summary>
		public IList<Site> TopList { get; set; }

		/// <summary>
		/// Flag welches angibt, ob Fehlermeldungen mitgeschrieben werden sollen in den in TracePath angegebenen Pfad
		/// </summary>
		public bool Trace { get; set; }

		/// <summary>
		/// Pfad zur TraceLog-Datei
		/// </summary>
		public string TracePath { get; set; }

		/// <summary>
		/// Pfad zu den Client-ASPX und ASMX Dateien
		/// </summary>
		public string WebSubPath { get; set; }

		/// <summary>
		/// Statische Methode zur Initialisierung mit Standardsettings
		/// </summary>
		/// <returns></returns>
		public static OrbCCSettings DefaultCrmClientCacherSettings()
		{
			OrbCCSettings settings = new OrbCCSettings();

			settings.CachingTimer = 70;
			settings.HostCheckTimer = 60;
			settings.InitialCacheWaitTime = 0;
			settings.SmartCache = true;
			settings.Threads = 10;
			settings.Throttle = 25;
			settings.Trace = false;
			settings.TracePath = Path.Combine(Path.GetTempPath(), "OrbClientCacher.log");
			settings.WebSubPath = @"res\web";

			settings = OrbCCSettings.InitializeLists(settings);

			return settings;
		}

		/// <summary>
		/// Initialisierung der Black-, Etc- und Top-Liste mit den Standardwerten
		/// </summary>
		/// <param name="settings">OrbCCSettings-Objekt</param>
		/// <returns>OrbCCSettings-Objekt</returns>
		private static OrbCCSettings InitializeLists(OrbCCSettings settings)
		{

			// Vorinitialisierung der BlackList
			settings.BlackList = new List<string>(1);
			settings.BlackList.Add("biz");

			// Vorinitialisierung der EtcListe
			settings.EtcList = new List<int>(10);
			settings.EtcList.Add(1);		// Account
			settings.EtcList.Add(2);		// Contact
			settings.EtcList.Add(3);		// Opportunity
			settings.EtcList.Add(4);		// Lead
			settings.EtcList.Add(135);		// ActivityParty
			settings.EtcList.Add(4200);		// ActivityPointer
			settings.EtcList.Add(4201);		// Appointment
			settings.EtcList.Add(4202);		// E-Mail
			settings.EtcList.Add(4210);		// Phone-Call
			settings.EtcList.Add(4212);		// Task

			// Vorinitialisierung der TopList
			settings.TopList = new List<Site>(53);

			settings.TopList.Add(new Site()
			{
				DoEtc = true,
				Priority = 1,
				Url = "main.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = true,
				Priority = 1,
				Url = "userdefined/edit.aspx"
			});

			#region TopListPrio2
			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/entityproperties/entitypropertiesutil.js.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/error/dlg_scriptError.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/icon.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/styles/dialogs.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/styles/fonts.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/styles/global.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/styles/main.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/styles/menucore.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/styles/miniCal.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/styles/select.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_common/styles/theme.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/attachment/attach.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/CompositeControl/compositecontrol.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/CompositeControl/CompositeControl.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/datenavigationbar/datenavigationbar.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/lookup/lookupdialogs.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/lookup/lookupinfo.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/monthcalendar/monthcalendar.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/multipicklist/multipicklist.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/notes/notectrl.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/notes/notesdata.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/notifications/notifications.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_controls/ribbon/RibbonLayout.js.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_forms/controls/controls.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_forms/controls/form.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_forms/FormScript.js.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_grid/appgrid.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_grid/RenderGridView.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_nav/menu.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_nav/nav.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "_nav/tabs.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = true,
				Priority = 2,
				Url = "_root/homepage.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "advancedfind/advfind.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "appnav/appnavbar.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "calendar/calendar.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "calendar/Day.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "calendar/Month.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "calendar/monthlycalendardata.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "CRMReports/rsviewer/reportviewer.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "crmreports/viewer/filtereditor.css.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "crmreports/viewer/viewer.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "dashboards/dashboard.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "Visualization/visualization.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "workplace/home_calendar.aspx"
			});

			settings.TopList.Add(new Site()
			{
				DoEtc = false,
				Priority = 2,
				Url = "workplace/home_dashboards.aspx"
			});
			#endregion

			settings.AsmxIndex = new IteratingList<string>(10);

			settings.AsmxIndex.Add("AppWebServices/AdvancedFind.asmx");
			settings.AsmxIndex.Add("AppWebServices/AppGridWebService.asmx");
			settings.AsmxIndex.Add("AppWebServices/AppGridWebService.asmx");
			settings.AsmxIndex.Add("AppWebServices/LookupMruWebService.asmx");
			settings.AsmxIndex.Add("AppWebServices/MessageBar.asmx");
			settings.AsmxIndex.Add("AppWebServices/PaneWebService.asmx");
			settings.AsmxIndex.Add("AppWebServices/RecentlyViewedWebService.asmx");

			return settings;
		}

	}
}
